package com.chixing.ih.module.programmer.service.Impl;

import com.chixing.ih.module.programmer.mapper.ProgrammerMapper;
import com.chixing.ih.module.programmer.pojo.Programmer;
import com.chixing.ih.module.programmer.pojo.dto.*;
import com.chixing.ih.module.programmer.service.IProgrammerService;
import com.chixing.ih.util.DateUtil;
import com.chixing.ih.util.EncryptUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘岩
 * Date: 2020-10-01
 * Time: 21:06
 *
 * @version 1.0
 */
@Service
@Transactional
public class ProgrammerServiceImpl implements IProgrammerService {

    @Autowired
    ProgrammerMapper programmerMapper;

    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;

    @Override
    public boolean savePersonalInfo(Programmer programmer) {
        int record = programmerMapper.updateOneByInfo(programmer);
        if (record > 0)
            return true;
        else
            return false;
    }

    @Override
    public boolean saveAccountInfo(Programmer programmer) {
        int record = programmerMapper.updateOne(programmer);
        if (record > 0)
            return true;
        else
            return false;
    }

    @Override
    public long listAllNum() {
        long programmerNum = programmerMapper.listAllNum();
        return programmerNum;
    }

    @Override
    public Programmer getProgrammerInfo(long programmerId) {
        Programmer programmer = programmerMapper.getOne(programmerId);
        return programmer;
    }

    @Override
    public boolean updatePassword(ProgrammerDTO programmerDTO) {
        Programmer programmerMapperOne = programmerMapper.getOne(programmerDTO.getProgrammerId());
        if (EncryptUtil.encrypt(programmerDTO.getOriginalPassword(), null).equals(programmerMapperOne.getProgrammerPassword())) {
            programmerDTO.setNewPassword(EncryptUtil.encrypt(programmerDTO.getNewPassword(), null));
            int record = programmerMapper.updatePassword(programmerDTO);
            if (record > 0)
                return true;
            else
                return false;
        }
        return false;

    }

    @Override
    public boolean savePerIntroduce(Programmer programmer) {
        int record = programmerMapper.updatePerIntroduce(programmer);
        if (record > 0)
            return true;
        else
            return false;
    }

    @Override
    public boolean updateProgrammerTime(long programmerId) {
        String currTime = DateUtil.getCurrTime("yyyy-MM-dd HH:mm:ss");
        Date date = DateUtil.convertDateStrToDate(currTime, "yyyy-MM-dd HH:mm:ss");
        int record = programmerMapper.updateTime(programmerId, date);
        if (record > 0)
            return true;
        return false;
    }

    @Override
    public ProgrammerAccountDTO getProgrammerAccount(long programmerId) {
        ProgrammerAccountDTO programmerAccountDTO = new ProgrammerAccountDTO();
        Programmer programmerMapperOne = programmerMapper.getOne(programmerId);
        programmerAccountDTO.setAlipayAccount(programmerMapperOne.getAlipayAccount());
        programmerAccountDTO.setQqAccount(programmerMapperOne.getQqAccount());
        programmerAccountDTO.setWechatAccount(programmerMapperOne.getWechatAccount());
        programmerAccountDTO.setProgrammerId(programmerMapperOne.getProgrammerId());
        return programmerAccountDTO;
    }

    @Override
    public PerIntroduceDTO getPerIntroduce(long programmerId) {
        PerIntroduceDTO perIntroduce = programmerMapper.getPerIntroduce(programmerId);
        return perIntroduce;
    }


    public List<Map<String, Object>> searchPage(String keyword, int pageNo, int pageSize) throws Exception {
        if (pageNo < 1) {
            pageNo = 1;
        }
        if (pageSize < 1) {
            pageSize = 20;
        }

        // 条件搜索
        SearchRequest searchRequest = new SearchRequest("user");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        // 分页实现
        sourceBuilder.from(pageNo);
        sourceBuilder.size(pageSize);
        // 精准匹配
        MatchQueryBuilder user_name = QueryBuilders.matchQuery("user_career", keyword);
        sourceBuilder.query(user_name);
        sourceBuilder.timeout(new TimeValue(240, TimeUnit.SECONDS));
        // 执行搜索
        searchRequest.source(sourceBuilder);
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
        // 解析结果

        ArrayList<Map<String, Object>> maps = new ArrayList<>();
        for (SearchHit hit : search.getHits().getHits()) {
            maps.add(hit.getSourceAsMap());
        }
        return maps;
    }

    @Override
    public List<ProgrammerHomeDTO> getProgrammers() {
        return programmerMapper.getProgrammers();
    }


    @Override
    public List<RecommendProgrammer> getRecommend(String userCareer, Integer userId) {
        List<RecommendProgrammer> recommend = programmerMapper.getRecommend(userCareer, userId);
        if (recommend.size() <= 4) {
            return recommend;
        } else {
            List<RecommendProgrammer> recommendList = new ArrayList<>();
            while (recommendList.size() < 4) {
                int i = (int) (Math.random() * (recommend.size()));
                RecommendProgrammer recommendProgrammer = recommend.get(i);
                if (recommendList.indexOf(recommendProgrammer) < 0) {
                    recommendList.add(recommendProgrammer);
                }

            }
            return recommendList;
        }

    }


}
